<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="患者姓名" prop="s_name">
        <el-input
          v-model="queryParams.s_name"
          placeholder="请输入患者姓名"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="s_phone">
        <el-input
          v-model="queryParams.s_phone"
          placeholder="请输入手机号"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="s_idnumber">
        <el-input
          v-model="queryParams.s_idnumber"
          placeholder="请输入身份证号"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          v-if="total == 1"
          @click="handleAdd"
        >查看档案</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="single"
          v-if="total != 1"
          @click="handleAdd"
        >查看档案</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          v-if="total != 1"
          @click="handleUpdate"
        >查看就诊病例</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          v-if="total == 1"
          @click="handleUpdate"
        >查看就诊病例</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" border :data="roleList" width="100%" @selection-change="handleSelectionChange">
      <el-table-column label="姓名" prop="s_name" />
      <el-table-column label="电话" prop="s_phone"  />
      <el-table-column label="身份证号" prop="s_idnumber"  />
      <el-table-column label="出生年月" prop="s_birth"/>
      <el-table-column label="年龄" prop="s_age" />
      <el-table-column label="性别" prop="s_sex"  />
      <el-table-column label="信息状态" prop="s_status"  />
      <el-table-column label="创建时间" prop="s_create_time"  />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看档案 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="s_name">
          <el-input v-model="form.s_name" readonly="readonly"  />
        </el-form-item>
        <el-form-item label="身份证号" prop="s_idnumber">
          <el-input v-model="form.s_idnumber" readonly="readonly" />
        </el-form-item>
        <el-form-item label="手机号" prop="s_phone">
          <el-input v-model="form.s_phone" readonly="readonly" />
        </el-form-item>
        <el-form-item label="性别" prop="s_sex">
          <el-input v-model="form.s_sex" readonly="readonly" />
        </el-form-item>
        <el-form-item label="出生日期" prop="s_birth">
          <el-input v-model="form.s_birth" readonly="readonly" />
        </el-form-item>
        <el-form-item label="年龄" prop="s_age">
          <el-input v-model="form.s_age" readonly="readonly" />
        </el-form-item>
        <el-form-item label="地址" prop="s_address">
          <el-input v-model="form.s_address" readonly="readonly" />
        </el-form-item>
        <el-form-item label="状态" prop="s_status">
          <el-input v-model="form.s_status" readonly="readonly" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 查看就诊病例 -->
    <el-dialog :title="title" :visible.sync="openDataScope" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="发病日期" prop="s_date">
          <el-input v-model="form.s_date" readonly="readonly"  />
        </el-form-item>
        <el-form-item label="接诊类型" prop="s_type">
          <el-input v-model="form.s_type" readonly="readonly" />
        </el-form-item>
        <el-form-item label="是否传染" prop="s_iscontagion">
          <el-input v-model="form.s_iscontagion" readonly="readonly" />
        </el-form-item>
        <el-form-item label="主诉" prop="s_complaint">
          <el-input v-model="form.s_complaint" readonly="readonly" />
        </el-form-item>
        <el-form-item label="诊断信息" prop="s_diagnostic">
          <el-input v-model="form.s_diagnostic" readonly="readonly" />
        </el-form-item>
        <el-form-item label="医生建议" prop="s_advice">
          <el-input v-model="form.s_advice" readonly="readonly" />
        </el-form-item>
        <el-form-item label="备注" prop="s_remark">
          <el-input v-model="form.s_remark" readonly="readonly" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelDataScope">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//import { listRole, getRole, delRole, addRole, updateRole, exportRole, dataScope, changeRoleStatus } from "@/api/visits/warehouse";
import { listRole, getRole, lokRole, addRole, updateRole, exportRole, dataScope, changeRoleStatus } from "@/api/visits/warehouse";
import { treeselect as menuTreeselect, roleMenuTreeselect } from "@/api/system/menu";
import { treeselect as deptTreeselect, roleDeptTreeselect } from "@/api/system/dept";

export default {
  name: "Role",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      roleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层（数据权限）
      openDataScope: false,
      menuExpand: false,
      menuNodeAll: false,
      deptExpand: true,
      deptNodeAll: false,
      // 日期范围
      dateRange: [],
      // 状态数据字典
      statusOptions: [],
      // 数据范围选项
      dataScopeOptions: [
        {
          value: "1",
          label: "全部数据权限"
        },
        {
          value: "2",
          label: "自定数据权限"
        },
        {
          value: "3",
          label: "本部门数据权限"
        },
        {
          value: "4",
          label: "本部门及以下数据权限"
        },
        {
          value: "5",
          label: "仅本人数据权限"
        }
      ],
      // 菜单列表
      menuOptions: [],
      // 部门列表
      deptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
          s_name: undefined,
      },
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单校验
      rules: {

      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询角色列表 */
    getList() {
      this.loading = true;
      listRole(this.queryParams).then(
        response => {
          this.roleList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 查询菜单树结构 */
    getMenuTreeselect() {
      menuTreeselect().then(response => {
        this.menuOptions = response.data;
      });
    },
    /** 查询部门树结构 */
    getDeptTreeselect() {
      deptTreeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 所有菜单节点数据
    getMenuAllCheckedKeys() {
      // 目前被选中的菜单节点
      let checkedKeys = this.$refs.menu.getCheckedKeys();
      // 半选中的菜单节点
      let halfCheckedKeys = this.$refs.menu.getHalfCheckedKeys();
      checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
      return checkedKeys;
    },
    // 所有部门节点数据
    getDeptAllCheckedKeys() {
      // 目前被选中的部门节点
      let checkedKeys = this.$refs.dept.getCheckedKeys();
      // 半选中的部门节点
      let halfCheckedKeys = this.$refs.dept.getHalfCheckedKeys();
      checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
      return checkedKeys;
    },
    /** 根据角色ID查询菜单树结构 */
    getRoleMenuTreeselect(roleId) {
      return roleMenuTreeselect(roleId).then(response => {
        this.menuOptions = response.menus;
        return response;
      });
    },
    /** 根据角色ID查询部门树结构 */
    getRoleDeptTreeselect(roleId) {
      return roleDeptTreeselect(roleId).then(response => {
        this.deptOptions = response.depts;
        return response;
      });
    },
    // 角色状态修改
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$confirm('确认要"' + text + '""' + row.roleName + '"角色吗?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return changeRoleStatus(row.roleId, row.status);
        }).then(() => {
          this.msgSuccess(text + "成功");
        }).catch(function() {
          row.status = row.status === "0" ? "1" : "0";
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮（数据权限）
    cancelDataScope() {
      this.openDataScope = false;
      this.reset();
    },
    // 表单重置
    reset() {
      if (this.$refs.menu != undefined) {
        this.$refs.menu.setCheckedKeys([]);
      }
      this.menuExpand = false,
      this.menuNodeAll = false,
      this.deptExpand = true,
      this.deptNodeAll = false,
      this.form = {
        roleId: undefined,
          s_id: undefined,
          s_name: undefined,
          s_phone: undefined,
          s_idnumber:undefined,
          s_sex: undefined,
          s_birth: undefined,
          s_age:undefined,
          s_address: undefined,
          s_status: undefined,
          s_date: undefined,
          s_type:undefined,
          s_iscontagion: undefined,
          s_complaint: undefined,
          s_diagnostic:undefined,
          s_advice: undefined,
          s_remark: undefined,
        roleSort: 0,
        status: "0",
        menuIds: [],
        deptIds: [],
        menuCheckStrictly: true,
        deptCheckStrictly: true,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
  /*  /!** 查看档案按钮操作 *!/
    handleAdd() {
      this.reset();
        const s_idnumber = this.roleList.s_idnumber
        getRole(s_idnumber).then(response => {
            this.form = response.data;

        });
      this.open = true;
      this.title = "添加角色";
    },*/

      /** 查看病例按钮操作 */
      handleUpdate() {
          this.reset();
          const s_idnumber = this.roleList[0].s_idnumber
          lokRole(s_idnumber).then(response => {
              this.form = response.data;
              this.openDataScope = true;
              this.title = "修改角色";
          });
      },
      /** 查看档案按钮操作 */
      handleAdd() {
          this.reset();
          const s_idnumber = this.roleList[0].s_idnumber
          getRole(s_idnumber).then(response => {
              this.form = response.data;
              this.open = true;
              this.title = "修改角色";
          });
      },
  }
};
</script>
